<?php
/**
 * ShopEx licence edit by kim
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */


class b2c_mdl_business_basicmsg extends dbeav_model{
	var $has_many = array(
        'brand' => 'business_brand:replace'
    );

   public function save1($brand_ids,&$businessid){
        $bb_model = $this->app->model('business_brand');
        $brand_model = $this->app->model('brand');
        $db = kernel::database();
        $db->beginTransaction();
       //选择出所有供应商id下的has_bind N
        $ids = $bb_model->getList('brand_id',array('business_id'=>$businessid));
        if($ids){
            foreach($ids as $k=>$v){
                $newarr[] = $ids[$k]['brand_id']; 
            }

            $brand_model->update(array('has_bind'=>'N'),array('brand_id'=>$newarr));
            if(!$bb_model->delete(array('business_id'=>$businessid))){
                $db->rollBack();
                return false;
            }
		}
            
		//选中的id的brand更改为已经绑定 并且将brandid和budinessid放进数据库
		foreach($brand_ids as $v){
			$data["brand_id"] = $v;
			$data["business_id"] = $businessid;
			$data["register_time"] = time();
			if(!$bb_model->insert($data)){
				$db->rollBack();
				return false;
			}
		}

		if(!$this->toShare($brand_ids ,$businessid)){
			$db->rollBack();
			return false;
		}
		
		$bind = $brand_model->update(array('has_bind'=>'Y'),array('brand_id'=>$brand_ids));
		if(!$bind){
			$db->rollBack();
			return false;
		}
		$db->commit();
        
        return $bind;
    }

    //根据供应商的库存是否确认，和品牌是否确认来圈定商品是否确认
    //条件是，如果供应商需要确认，品牌不需要，则不需要;品牌需要则需要；否则，都不需要
    //edit20150813 kim
    public function toShare($ids,$businessid){
        $businessObj = $this->app->model('business_basicmsg');
        $brand_model = $this->app->model('brand');
        $goods_model = $this->app->model('goods');
        
        $businessArr = $businessObj->getRow('is_share',array('business_id'=>$businessid));
        //商品表里有business_id 和 brand_id
        if($businessArr['is_share']=='true'){
            $brands = $brand_model->getList('brand_id,sto_confirm',array('brand_id'=>$ids));
            foreach($brands as $v){
                if($v['sto_confirm']=='Y'){
                    $sto = $goods_model->getRow('sto_confirm',array('brand_id'=>$v['brand_id']));
                    if($sto['sto_confirm']=='Y'){
                        $goods_model->update1(array('goods_confirm'=>'Y'),array('brand_id'=>$v['brand_id']));
                    }else{
                        $goods_model->update1(array('goods_confirm'=>'N'),array('brand_id'=>$v['brand_id']));
                    }   
                }else{
                    $goods_model->update1(array('goods_confirm'=>'N'),array('brand_id'=>$v['brand_id']));
                }
            }
        }else{
            $goods_model->update1(array('goods_confirm'=>'N'),array('business_id'=>$businessid));
        }
        return true;
    }

    /**
	 * 供应商
	 * @param $business_id 
	 */
    public function get_all_business_orders($business_id){
        $sql = "SELECT A.*,D.first_createtime,F.num FROM sdb_b2c_business_basicmsg AS A JOIN (select min(C.createtime) as first_createtime,C.business_id from sdb_b2c_business_orders AS C  WHERE C.order_status<>'dead' GROUP BY C.business_id) AS D ON D.business_id=A.business_id LEFT JOIN (select count(E.obj_id) as num,E.business_id from sdb_b2c_business_orders_objects AS E where E.state='0' GROUP BY E.business_id HAVING count(E.obj_id)>0) AS F ON F.business_id=A.business_id WHERE A.business_id in(".$business_id.") order by A.business_id desc";
        return $this->db->select($sql);
    }

	function get_english_name($business_id){
		$list = $this->getRow("english_all_name",array('business_id'=>$business_id));
		return $list['english_all_name'];
	}

	public function modifier_member_id($row)
    {
        if ($row === 0 || $row == '0'){
            return app::get('b2c')->_('非会员顾客');
        }
        else{
            return kernel::single('b2c_user_object')->get_member_name(null,$row);
        }
    }
}